package cn.chainco.cts.iserviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.chainco.cts.idao.IWorkDao;
import cn.chainco.cts.iservice.IWorkManager;
import cn.chainco.cts.jdo.Company;
import cn.chainco.cts.jdo.ContainerNo;
import cn.chainco.cts.jdo.Currency;
import cn.chainco.cts.jdo.Employee;
import cn.chainco.cts.jdo.User;
import cn.chainco.cts.jdo.Work;
import cn.chainco.cts.jdo.WorkFee;
import cn.chainco.cts.jdo.WorkItem;
import cn.chainco.cts.utils.TimeUtil;

public class WorkManagerImpl extends CommonManagerImpl implements IWorkManager{
	private IWorkDao workDao;

	public void setWorkDao(IWorkDao workDao){
		this.workDao = workDao;
	}

	public int getCountByStatus( Company company,User user,Employee employee,int status){
		return this.workDao.getCountByStatus(company, user, employee,status);
	}
	public List<Work> getWorkByOrConditions( Company company,User user,Employee employee,Map<String,String> workConditions,Map<String,String> workItemConditions,Map<String,String> workTeamConditions,Map<String,String> tagConditions,int pageSize,int pageNumber){
		return this.workDao.getWorkByOrConditions(company, user,employee, workConditions, workItemConditions, workTeamConditions, tagConditions,pageSize,pageNumber);
	}
	public List<Work> getWorkByAndConditions( Company company,User user,Employee employee,Map<String,String> workConditions,Map<String,String> workItemConditions,Map<String,String> workTeamConditions,Map<String,String> tagConditions,int pageSize,int pageNumber){
		return this.workDao.getWorkByAndConditions(company, user,employee, workConditions, workItemConditions, workTeamConditions, tagConditions,pageSize,pageNumber);
	}
	public List<WorkFee> getFeesByWork( Work work,int type ){
		return this.workDao.getFeesByWork(work, type);
	}
	public List<WorkItem> getItemsByWork( Work work ){
		return this.workDao.getItemsByWork(work);
	}
	
	/**
	 * 获取某家公司指定创建月份的订单总数
	 */
	public int getCountByCompany(Company company, Date date){
		return this.workDao.getCountByCompany(company, date);
	}
	
	/**
	 * 根据订单号查询订单
	 */
	public Work getWorkByWorkCode(Company company, String workCode){
		return this.workDao.getWorkByWorkCode(company, workCode);
	}

	/**
	 * 验证订单号是否存在
	 */
	public boolean validateWorkCode(Company company, String workCode){
		return this.getWorkByWorkCode(company, workCode) != null;
	}
	
	/**
	 * 生成订单号
	 */
	public String getNewWorkCode(Company company){
		int count = this.getCountByCompany(company, new Date());
		String workCode = "";
		boolean isTrue = true;
		while(isTrue){
			count++;
			String index = count+"";
			if(count < 100) index = "0" + index;
			if(count < 10) index = "0" + index;
			workCode = TimeUtil.getYYMM(new Date()) + index;
			if(!this.validateWorkCode(company, company.getPrefix() + workCode)){
				isTrue = false;
			}
		}
		return workCode;
	}
	/**
	 * 添加箱量柜号
	 * @param map key是类型，value是数量
	 * @param work
	 */
	public Map<Integer, Integer> addBoxAmount(Map<Integer, Integer> map, Work work){
		Map<Integer, Integer> mapCount = this.getBoxTypeCount(work);
		Map<Integer, Integer> resultCount = new HashMap<Integer, Integer>();
		for (Integer key : map.keySet()) {
			if(mapCount.get(key) != null && map.get(key) > mapCount.get(key)){
				map.put(key, map.get(key) - mapCount.get(key));
			}else if(mapCount.get(key) != null){
				if(mapCount.get(key) > map.get(key)){
					int count = mapCount.get(key) - map.get(key);
					int result = this.workDao.delBoxByEmpty(work, key.toString(), count);
					if(count != result){
						resultCount.put(key, count - result);
					}
				}
				map.put(key, 0);
			}
		}
		this.workDao.addBoxAmount(map, work);
		return resultCount;
	}
	/**
	 * 统计箱量类型数量
	 * @param work key是类型，value是数量
	 * @return
	 */
	public Map<Integer, Integer> getBoxTypeCount(Work work){
		return this.workDao.getBoxTypeCount(work);
	}
	/**
	 * 获取订单全部箱量
	 */
	public List<ContainerNo> getAllContainerNoByWork(Work work){
		return this.workDao.getAllContainerNoByWork(work);
	}
	/**
	 * 根据地点类型查询其它委托项目，地点类型为null查询所有
	 */
	public List<WorkItem> getWorkItemByType(Work work, Integer type){
		return this.workDao.getWorkItemByType(work, type);
	}
	/**
	 * 更新Work状态
	 */
	public void updateWorkStatus( Work work ){
		//如果是暂停或者归档，为手动触发更改，直接返回
		if( work.getWorkingStatus() == 5 ) return;
		this.workDao.updateWorkStatus(work);
	}
	
	/**
	 * 获取某人在某家公司的订单的某种单的总数
	 */
	public int getCountByType( Company company,User user,Employee employee,String itemName ){
		return this.workDao.getCountByType(company, user,employee, itemName);
	}
	/**
	 * 统计订单
	 * statisticalType ：统计类型 1-统计奖励,2-统计毛利,3-统计订单
	 * conditions : 条件，统计结束后会多一个Key为workCount的总数量
	 */
	public List<Work> statisticalWorks(Company company, int statisticalType, boolean isGroup, Map<String, Object> conditions, int pageSize, int pageNumber){
		return this.workDao.statisticalWorks(company, statisticalType, isGroup, conditions, pageSize, pageNumber);
	}
	
	/**
	 * <pre>
	 * 统计订单费用，返回float数组，保留二位小数
	 * float[0]为应收费用合计，float[1]为应付费用合计，float[2]为奖励合计，float[3]为毛利润，float[4]为公司利润
	 */
	public float[] countWorkFees(Work work, Currency currency){
		return this.workDao.countWorkFees(work, currency);
	}

	/**
	 * 统计奖励费用A奖励+B奖励,并保存到字段totalReward
	 */
	public float countWorkFee(WorkFee workFee, Currency currency){
		return this.workDao.countWorkFee(workFee, currency);
	}
	/**
	 * 财务统计(应收未收应付未付)
	 * conditions : 条件，统计结束后会多一个Key为workCount的总数量
	 * statisticsType=0应收未收, statisticsType=1应付未付
	 */
	public List<WorkFee> statisticsFinancial(Company company, int statisticsType, String clientId, String months, String days, 
		String employeeId, String tag, String workingStatus, int pageSize, int pageNumber){
		return this.workDao.statisticsFinancial(company, statisticsType, clientId, months, days, employeeId, tag, workingStatus, pageSize, pageNumber);
	}
	
	/***
	 * 根据传入的workId更新最新的和钱有关的状态并且更新其有比例奖励的数据的
	 */
	public void updateWorkFeesStatus( long workId ){
		this.workDao.updateWorkFeesStatus(workId);
	}
}
